package com.gxa.meiye.portal.controller.zy;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.gxa.meiye.core.common.JsonResult;
import com.gxa.meiye.core.pojo.domain.Order;
import com.gxa.meiye.portal.service.zy.OrderService;
import com.gxa.meiye.portal.service.zy.PayService;
import com.gxa.meiye.portal.utils.PayUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @Author: zy
 * @CreateTime: 2022-07-21  10:05
 */
@RestController
@Slf4j
@Api(value = "支付",tags = "上门服务支付接口")
@RequestMapping("/comepay")
public class PayController {
  @Autowired
  private PayUtil payUtil;
  @Autowired
  private OrderService orderService;
  @Autowired
  private PayService payService;
  private static String  orderNumber=null;
    //支付接口
    @ApiOperation(value = "支付")
    @GetMapping("/paymoney")
    public String pay(Order order) throws JsonProcessingException {
        String s = UUID.randomUUID().toString().replaceAll("-", "");
        String pay = payUtil.pay(s,order.getPayMoney().toString());
        orderNumber=order.getOrderNumber();
        //JsonResult jsonResult=new JsonResult();
        if (!StringUtils.isEmpty(pay)){
            orderService.update(order.getOrderNumber(),s);
        }
//            jsonResult.setCode("0");
//            jsonResult.setMsg("支付失败");
//            return jsonResult.getMap();
//        }
//        jsonResult.setCode("1");
//        jsonResult.setMsg("支付成功");
//        return jsonResult.getMap();
        return pay;
    }
    @ApiOperation(value = "payreturn")
    @RequestMapping("/payReturn")
    public Map payReturn(HttpServletRequest request){
        //Map<String,String> map=new HashMap<>();
        Map<String, String[]> parameterMap = request.getParameterMap();
        Set<Map.Entry<String, String[]>> entries = parameterMap.entrySet();
        Iterator<Map.Entry<String, String[]>> iterator = entries.iterator();
        Map<String,String[]> map=new HashMap<>();
        while (iterator.hasNext()){
            Map.Entry<String, String[]> next = iterator.next();
            log.info("key{},value{}",next.getKey(),next.getValue());
            map.put(next.getKey(),next.getValue());
        }
        String[] number= map.get("out_trade_no");
        orderService.update(orderNumber,number[0]);
        return null;
    }
    //退款
    @ApiOperation(value = "退款")
    @GetMapping("/refund")
    public Map refund(String orderNumber,String money){
        String refund = payService.refund(orderNumber);
        String send = payUtil.send(refund, money);
        JsonResult jsonResult=new JsonResult();
        if (send.equals("退款成功")){
            //调用后台修改状态
            Integer integer = payService.refundUpdate(orderNumber);
            jsonResult.setCode("1");
            if (integer==1) {

                jsonResult.setMsg("退款成功");
            }else {
                jsonResult.setMsg("状态修改失败");
            }
            return jsonResult.getMap();
        }
        jsonResult.setCode("0");
        jsonResult.setMsg("退款失败");
        return jsonResult.getMap();
    }
}
